<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:doc="livcos.org/ns/doc"
	xmlns:c="livcos.org/ns/cosmos"
	xmlns:m="livcos.org/ns/map"
	xmlns:pm="livcos.org/ns/pm"
	xmlns:u="livcos.org/ns/cosmos/update"
  xmlns:job="livcos.org/ns/process/job"
  xmlns="livcos.org/ns/doc"
  exclude-result-prefixes="c m doc">

  <xsl:import href="../../style/base.xsl" />
  
  <xsl:param name="objId"/>
  <xsl:param name="scope" select="'content'"/>
  <xsl:param name="id"/>
  <xsl:param name="view"/>

  <xsl:template match="/*">
    <u:context ref="{$objId}" scope="{$scope}">
      <u:context select="*//*[@id = '{$id}']">
        <xsl:apply-templates select="c:request/parameter/transform" />
      </u:context>
    </u:context>
    <job:transform def="/livcos.org/web/editor/integrity.xsl">
      <job:param name="obj" value="{$objId}"/>
    </job:transform>
    <job:feed ref="{$view}" stream="response" base="job"/>
  </xsl:template>
	
	<xsl:template match="transform">
		<xsl:variable name="transformed">
			<xsl:apply-templates select="doc(concat($objId, '.', $scope))//*[@id = $id]"/>
		</xsl:variable>
		<xsl:if test="$transformed/*">
			<u:replace select=".">
				<xsl:copy-of select="$transformed"/>
			</u:replace>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="@* | node()" mode="copy">
		<xsl:copy>
			<xsl:apply-templates select="@* | node()" mode="#current"/>
		</xsl:copy>
	</xsl:template>
	
	<xsl:template match="@* | node()">
	</xsl:template>
	
	<xsl:template match="doc:paragraph[starts-with(., 'TODO')]">
		<pm:task id="{{new-id()}}" priority="3">
			<pm:title>
				<xsl:variable name="txt" select="text()[1]"/>
				<xsl:variable name="pos">
					<xsl:choose>
						<xsl:when test="starts-with($txt, 'TODO: ')">7</xsl:when>
						<xsl:when test="starts-with($txt, 'TODO:')">6</xsl:when>
						<xsl:when test="starts-with($txt, 'TODO')">5</xsl:when>
					</xsl:choose>
				</xsl:variable>
				<xsl:value-of select="substring($txt, $pos)"/>
				<xsl:apply-templates select="node()[position() > 1]" mode="copy"/>
			</pm:title>
		</pm:task>
	</xsl:template>
	
	<xsl:template match="m:item/doc:link">
		<paragraph id="{{new-id()}}">
			<xsl:copy>
				<xsl:apply-templates select="@* | node()" mode="copy"/>
			</xsl:copy>
		</paragraph>
	</xsl:template>
	
	<xsl:template match="m:map/m:body/m:item">
	</xsl:template>
	
	<xsl:template match="m:item">
		<chapter id="{{new-id()}}">
			<title><xsl:value-of select="m:label"/></title>
			<xsl:if test="@base">
				<paragraph id="{{new-id()}}">
					<xsl:text>See </xsl:text>
					<link id="{{new-id()}}" ref="/livcos.org/Map/web/nav.html?map={replace(@base, '#', '&amp;item=')}">
						<xsl:variable name="map" select="doc(concat(substring-before(@base, '#'), '.content'))/*"/>
						<xsl:text>item in </xsl:text>
						<xsl:value-of select="$map/m:head/m:title"/>
					</link>
					<xsl:text>.</xsl:text>
				</paragraph>
			</xsl:if>
			<xsl:apply-templates select="* except m:*" mode="copy"/>
			<xsl:apply-templates select="m:rels"/>
		</chapter>
	</xsl:template>
	
	<xsl:template match="m:rels">
		<xsl:apply-templates select="m:*"/>
	</xsl:template>
	
	<xsl:template match="m:map/m:body/m:item/m:rels/m:rel">
	</xsl:template>
	
	<xsl:template match="m:rel">
		<xsl:variable name="uri">
			<xsl:if test="starts-with(@ref, '#')">
				<xsl:choose>
					<xsl:when test="../../@base">
						<xsl:value-of select="substring-before(../../@base, '#')"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="base-uri()"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:if>
			<xsl:value-of select="@ref"/>
		</xsl:variable>
		<xsl:variable name="map" select="doc(concat(substring-before($uri, '#'), '.content'))/*"/>
		<xsl:variable name="item" select="$map/m:body/*[@id = substring-after($uri, '#')]"/>
		<chapter id="{{new-id()}}">
			<title><xsl:value-of select="$item/m:label"/></title>
			<xsl:if test="normalize-space(.) != ''">
				<paragraph id="{{new-id()}}"><xsl:value-of select="."/></paragraph>
			</xsl:if>
			<xsl:apply-templates select="$item/* except $item/m:*" mode="copy"/>
		</chapter>
	</xsl:template>
	
</xsl:stylesheet>
